package cn.yuznu.www;

import java.util.Arrays;

public class MyStack {
    private int[] elem;
    private int top;
    private int count;//栈大小

    public MyStack(int count){//创建一个栈
        this.count=count;
        this.elem=new int[count];
        top=-1;
    }

    public void push(int val){
        if(isFull()){
            this.elem= Arrays.copyOf(elem,2*elem.length);//扩容
        }
        elem[++top]=val;//入栈
    }

    public int pop(){//出栈
        if(isNULL()){
            System.out.println("栈已空");
            return 0;
        }
        return elem[top--];
    }
    public int peek() {//查看栈顶元素
        if (isNULL()) {
            return 0;
        }
        return elem[top];
    }
    public boolean isFull(){//满了
        return top+1==count;
    }

    public boolean isNULL(){//空栈
        return top==-1;
    }
}
